package com.example.finaltest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.example.finaltest.fuzzysearch.FuzzySearchLeaderActivity;
import com.example.finaltest.goods.GoodsTypeFragment;
import com.example.finaltest.goods.TypeAdapter;
import com.example.finaltest.goods.TypeBean;
import com.example.finaltest.utils.BaseFragment;
import com.example.finaltest.utils.URLUtils;
import com.example.finaltest.R;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
/**
 * 分类的碎片
 */
public class TypeFragment extends BaseFragment {
    ListView typeLv;
    EditText searchEt;   //搜索框
    List<TypeBean.DataBean> data;  //数据源
    private TypeAdapter adapter;
    private FragmentManager fm;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_type, container, false);
        initView(view);
        fm = getChildFragmentManager();

        //2.确定数据源
        data = new ArrayList<>();
        //3.设置适配器
        adapter = new TypeAdapter(getContext(), data);
        typeLv.setAdapter(adapter);
//        4.连接网络
        getNetwork(URLUtils.queryAllCategory_url);
        setListener();
        return view;
    }

    private void changeRightPage(int position) {
        TypeBean.DataBean dataBean = data.get(position);
        int id = dataBean.getId();
        FragmentTransaction transaction = fm.beginTransaction();
        GoodsTypeFragment gtfrag = GoodsTypeFragment.newInstance(id + "", "");
        transaction.replace(R.id.frag_type_layout, gtfrag);
        transaction.commit();
    }

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        TypeBean typeBean = new Gson().fromJson(result, TypeBean.class);//解析数据
        List<TypeBean.DataBean> list = typeBean.getData();//获取网络数据集合
        data.addAll(list);  //将网络数据添加到list view的数据源当中
        adapter.notifyDataSetChanged(); //提示更新
        changeRightPage(0);
    }

    private void initView(View view) {
        typeLv = view.findViewById(R.id.frag_type_lv);
        searchEt = view.findViewById(R.id.search_et);
        searchEt.setOnClickListener(listener);
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.search_et:
                    Intent intent_fs = new Intent(getContext(), FuzzySearchLeaderActivity.class);
                    startActivity(intent_fs);
                    break;
            }
        }
    };

        public void setListener() {
            typeLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    adapter.setClickPos(position);
                    changeRightPage(position);
                }

            });
        }
    }
